<?php
/**
 * 
 * 默认提供的分页绘制器
 * @author LeoNing
 *
 */
class DefaultPagingRender implements IPagingRender {
	public function render($params, $options) {
		$total = $params ['total'];
		$url = $params ['url'];
		$cur = $params ['cur'];
		$limit = $params ['per'];
		$ext = $params ['ext'];
		$pp = 10;
		if ($total == 0) {
			return '';
		}
		$tp = ceil ( $total / $limit ); //一共有多少页
		$pager = array ('<div class="pager"><div class="flt"><span>' );
		$pager [] = sprintf ( '共%s条记录,每页%d条记录', $total, $limit );
		$pager [] = '</span></div><div class="frt">';
		$_cp = $cur * $limit;
		$_cp = $_cp > $total ? $total : $_cp;
		$pager [] = sprintf ( '<span>第%s~%s条记录</span>', ($cur - 1) * $limit + 1, $_cp );
		if ($tp > 1) {
			if ($cur == 1) { //当前在第一页
				$pager [] = '<span>首</span><span>上</span>';
			} else {
				$pager [] = sprintf ( '<a title="第一页" href="%s">首</a><a title="上一页" href="%s">上</a>', $url . '1' . $ext, $url . ($cur - 1) . $ext );
			}
			//向前后各多少页
			$sp = $pp % 2 == 0 ? $pp / 2 : ($pp + 1) / 2;
			if ($cur <= $sp) {
				$start = 1;
				$end = $sp * 2 + 1;
				$end = $end > $tp ? $tp : $end;
			} else {
				$start = $cur - $sp;
				$end = $cur + $sp;
				if ($end >= $tp) {
					$start -= ($end - $tp);
					$start > 0 or $start = 1;
					$end = $tp;
				}
			}
			for($i = $start; $i <= $end; $i ++) {
				if ($i == $cur) {
					$pager [] = sprintf ( '<b>%d</b>', $i );
				} else {
					$pager [] = sprintf ( '<a href="%s" title="第%d页">%d</a>', $url . $i . $ext, $i, $i );
				}
			}
			if ($cur == $tp) {
				$pager [] = '<span>下</span><span>尾</span>';
			} else {
				$pager [] = sprintf ( '<a title="下一页" href="%s">下</a><a title="最后一页" href="%s">尾</a>', $url . ($cur + 1) . $ext, $url . $tp . $ext );
			}
		}
		$pager [] = '</div></div>';
		return implode ( "", $pager );
	}
}